﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Parts_Cart : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Act"] != null && Request.QueryString["Act"].Equals("-1"))
        {
            try
            {
                int id = int.Parse(Request.QueryString["IID"]);

                List<TaiLieu> lst = new List<TaiLieu>();
                if (Session["Cart"] != null)
                {
                    lst = (List<TaiLieu>)Session["Cart"];
                }

                for (int i = 0; i < lst.Count; i++)
                {
                    if (lst[i].ProductID == id)
                    {
                        lst.RemoveAt(i);
                        Session["Cart"] = lst;
                    }
                }
                Response.Redirect("Cart.aspx");
            }
            catch
            {
            }
        }

        if (Request.Form["Act"] != null && Request.Form["Act"].ToString().Equals("1"))
        {
            try
            {
                List<TaiLieu> lst = new List<TaiLieu>();

                NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();

                int id = int.Parse(Request.Form["IID"]);

                if (Session["Cart"] == null)
                {

                    TaiLieu tl = (from sp in db.TaiLieus
                                  where sp.ProductID == id
                                  select sp).Single<TaiLieu>();

                    tl.ProductQuantity = 1;

                    lst.Add(tl);
                }
                else
                {
                    lst = (List<TaiLieu>)Session["Cart"];

                    int flag = 0;

                    for (int i = 0; i < lst.Count; i++)
                    {
                        if (lst[i].ProductID == id)
                        {
                            lst[i].ProductQuantity++;
                            flag = 1;
                        }
                    }

                    if (flag == 0)
                    {

                        TaiLieu tl = (from sp in db.TaiLieus
                                      where sp.ProductID == id
                                      select sp).Single<TaiLieu>();

                        tl.ProductQuantity = 1;
                        lst.Add(tl);
                    }
                }

                Session["Cart"] = lst;
            }
            catch
            {
            }
        }

        if (Request.Form["Act"] != null && Request.Form["Act"].ToString().Equals("0"))
        {
            try
            {
                List<TaiLieu> lst = new List<TaiLieu>();

                int id = int.Parse(Request.Form["IID"]);

                lst = (List<TaiLieu>)Session["Cart"];

                NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();

                for (int i = 0; i < lst.Count; i++)
                {
                    if (lst[i].ProductID == id)
                    {
                        var qty = Request.Form["qty"];
                        try
                        {
                            int sl = int.Parse(qty);

                            var query = (from sp in db.TaiLieus
                                         where sp.ProductID == id
                                         select sp).Single();
                            if (sl > 0)
                            {
                                if (sl > query.ProductQuantity)
                                {
                                    sl = (int)query.ProductQuantity;

                                    divMsgNotEnough.Visible = true;
                                }

                                lst[i].ProductQuantity = sl;
                            }
                        }
                        catch { }
                    }
                }

                Session["Cart"] = lst;
            }
            catch
            {
            }
        }
    }
}